package com.droidathletics.control;

import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.droidathletics.boundary.MainMenu;
import com.droidathletics.boundary.dao.MacrocycleDAOAdapter;
import com.droidathletics.entity.Date;
import com.droidathletics.entity.Macrocycle;
import com.droidathletics.entity.Sportsman;

import android.content.Context;
import android.database.Cursor;


public class ManageMacrocycle {
	public ManageMacrocycle(Context context)
	{
		macrocycleDAO = new MacrocycleDAOAdapter(context);
	}
	
	public MacrocycleDAOAdapter getMacrocycleDAO() {
		return macrocycleDAO;
	}

	public void setMacrocycleDAO(MacrocycleDAOAdapter macrocycleDAO) {
		this.macrocycleDAO = macrocycleDAO;
	}

	private MacrocycleDAOAdapter macrocycleDAO;

	public String createMacrocycle(String name, String dayStart, String monthStart, String yearStart, 
			String dayEnd, String monthEnd, String yearEnd, 
			String teamName, String branch, String category, String competition)
	{
		String validateInformation = checkBusinessRules(name, teamName, branch, category, competition);
		
		if ( validateInformation.equals("")) 
		{
		  Macrocycle newMacrocycle = new Macrocycle(name,
				  new Date(Integer.parseInt(dayStart), Integer.parseInt(monthStart), Integer.parseInt(yearStart)),
				  new Date(Integer.parseInt(dayEnd), Integer.parseInt(monthEnd), Integer.parseInt(yearEnd)),
				  teamName, branch, category, competition);
		
		  macrocycleDAO.createMacrocycle(name, dayStart, monthStart, yearStart, dayEnd, monthEnd, yearEnd,
				      teamName, branch, category, competition);
		
		}
		
		return validateInformation;
	}
		
	public String updateMacrocycle(Macrocycle oldMacrocycle, String name, 
			String dayStart, String monthStart, String yearStart, 
			String dayEnd, String monthEnd, String yearEnd, 
			String teamName, String branch, String category, String competition)
	{
		Macrocycle updMacrocycle = oldMacrocycle;
		String validateInformation = checkBusinessRules(name, teamName, branch, category, competition);
		
		if ( validateInformation.equals("")) 
		{
		    macrocycleDAO.updateMacrocycle(updMacrocycle.getName(), 
			name, dayStart, monthStart, yearStart, dayEnd, monthEnd, yearEnd, teamName, branch, category, competition );
		    
		return "";
		}
		
		return validateInformation;
	}
	
    public ArrayList<Macrocycle> readAllMacrocycle()
    {
        Cursor cursor = macrocycleDAO.readMacrocycle();
        ArrayList<Macrocycle> listDBMacrocycle = new ArrayList<Macrocycle>();
        MainMenu.drawablesMacrocycles.clear();
		if (cursor.moveToFirst()){
			do
			{
               String name = cursor.getString(cursor.getColumnIndex("name"));
               String startDate = cursor.getString(cursor.getColumnIndex("startdate"));
               Date startdate = transformDate(startDate);
               String endDate = cursor.getString(cursor.getColumnIndex("enddate"));
               Date enddate = transformDate(endDate);
               String teamname = cursor.getString(cursor.getColumnIndex("teamname"));
               String branch = cursor.getString(cursor.getColumnIndex("branch"));
               String category = cursor.getString(cursor.getColumnIndex("category"));
               String competition = cursor.getString(cursor.getColumnIndex("competition"));

               MainMenu.addDrawableMacrocycles();
               listDBMacrocycle.add( new Macrocycle(name, startdate, enddate, teamname, branch, category, competition) );

			} while ( cursor.moveToNext());

       }
       cursor.close();
       
       return listDBMacrocycle;
    }
    
    public ArrayList<Macrocycle> searchMacrocycles(String pam)
    {
    	Cursor cursor = macrocycleDAO.searchMacrocycle(pam);
        ArrayList<Macrocycle> listDBMacrocycle = new ArrayList<Macrocycle>();
        MainMenu.drawablesMacrocycles.clear();
		if (cursor.moveToFirst()){
			do
			{
               String name = cursor.getString(cursor.getColumnIndex("name"));
               String startDate = cursor.getString(cursor.getColumnIndex("startdate"));
               Date startdate = transformDate(startDate);
               String endDate = cursor.getString(cursor.getColumnIndex("enddate"));
               Date enddate = transformDate(endDate);
               String teamname = cursor.getString(cursor.getColumnIndex("teamname"));
               String branch = cursor.getString(cursor.getColumnIndex("branch"));
               String category = cursor.getString(cursor.getColumnIndex("category"));
               String competition = cursor.getString(cursor.getColumnIndex("competition"));

               MainMenu.addDrawableMacrocycles();
               listDBMacrocycle.add( new Macrocycle(name, startdate, enddate, teamname, branch, category, competition) );

			} while ( cursor.moveToNext());

       }
       cursor.close();
       
       return listDBMacrocycle;
    }
    
    private Date transformDate(String fullDate)
    {
      StringTokenizer token = new StringTokenizer(fullDate, "/");
      ArrayList<String> dateValues = new ArrayList<String>();
      do{
    	  dateValues.add(token.nextToken());
      }while(token.hasMoreTokens());
      
      return new Date(Integer.parseInt(dateValues.get(0)),
    		  Integer.parseInt(dateValues.get(1)),
    		  Integer.parseInt(dateValues.get(2)));
  	      
    }
    
    public boolean deleteMacrocycle(String name)
    {
    	macrocycleDAO.deleteMacrocycle(name);
    	return true;
    }	
    
	public String checkBusinessRules(String name, String teamName, String branch,
			String category, String competition)
	{
		
		if ( !checkNamesAreValid(name) )
		{
			return ("The name field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(teamName) )
		{
			return ("The team name field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(branch) )
		{
			return ("The branch field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(category) )
		{
			return ("The category field must be contain only characters.");
		}
		
		if ( !checkNamesAreValid(competition) )
		{
			return ("The competition field must be contain only characters.");
		}
		
		return ("");
	}
	
	private boolean checkNamesAreValid(String name)
	{
        Pattern patternName = Pattern.compile("[^A-Za-z������ ]");
        Matcher matcherName = patternName.matcher(name);

        boolean isString = false;
        while (matcherName.find())
        {
                isString = true;
        }
        return !isString;
	}
	
}
